﻿using System;

namespace MiniGert
{
  /// <summary>
  /// Point is a simple point in 2D space.
  /// </summary>
  public class Point
  {
    private float _x = 0;
    private float _y = 0;
    
    /// <summary>
    /// Creates a new Point.
    /// </summary>
    public Point()
    {
    }

    /// <summary>
    /// Creates a new Point initializing X and Y with the given values.
    /// </summary>
    /// <param name="x">The X coordinate of the point</param>
    /// <param name="y">The Y coordinate of the point</param>
    public Point(float x, float y)
    {
      _x = x;
      _y = y;
    }
    
    /// <summary>
    /// Gets or sets the X coordinate of the point.
    /// </summary>
    public float X
    {
      get { return _x; }
      set { _x = value; }
    }
    
    /// <summary>
    /// Gets or sets the Y coordinate of the point.
    /// </summary>
    public float Y
    {
      get { return _y; }
      set { _y = value; }
    }
    
    /// <summary>
    /// Transform the point in the given direction taking into account the factor.
    /// </summary>
    /// <param name="direction">The direction to move the point.</param>
    /// <param name="factor">A factor to scale the direction.</param>
    public void Transform(Vector direction, float factor)
    {
      if (factor != 1.0f)
      {
        _x += direction.X * factor;
        _y += direction.Y * factor;
      }
      else
      {
        _x += direction.X;
        _y += direction.Y;
      }
    }
    
    /// <summary>
    /// Calculates the distance between this point and another by using pythogarean theorem.
    /// </summary>
    /// <param name="other">The other point.</param>
    /// <returns>The distance between the two points.</returns>
    public float DistanceTo(Point other)
    {
      float a = _x - other._x;
      float b = _y - other._y;
      return (float)Math.Sqrt(a * a + b * b);
    }
    
    /// <summary>
    /// Calculates the distance between two points by using pythogarean theorem.
    /// </summary>
    /// <param name="p1">The first point.</param>
    /// <param name="p2">The second point.</param>
    /// <returns>The distance between the two points.</returns>
    public static float Distance(Point p1, Point p2)
    {
      float a = p1._x - p2._y;
      float b = p1._x - p2._y;
      return (float)Math.Sqrt(a * a + b * b);
    }

    /// <summary>
    /// Transforms the given point in the given direction. The components of the direction vector
    /// are multiplied by the given factor to get the distance to transform the point. This is only 
    /// done if the factor != 1.0f.
    /// The expression for the transform is <code>pr = point + (direction * factor)</code>.
    /// </summary>
    /// <param name="point">The point to be transformed.</param>
    /// <param name="direction">The direction to transform the point in.</param>
    /// <param name="factor">A factor that is aplied to the transformation.</param>
    /// <returns>A new point that is the given point transformed in the given direction.</returns>
    public static Point Transform(Point point, Vector direction, float factor)
    {
      Point result = new Point();
      if (factor != 1.0f)
      {
        result.X += point.X + direction.X * factor;
        result.Y += point.Y + direction.Y * factor;
      }
      else
      {
        result.X += point.X + direction.X;
        result.Y += point.Y + direction.Y;
      }

      return result;
    }
    
  }
}
